package com.example.demo.elasticsearch;

import com.example.demo.elasticsearch.mapper.GoodsMapper;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class WebController {

    @Resource
    private RestHighLevelClient client;

    @Autowired
    private GoodsMapper goodsMapper;

    @GetMapping
    // 查询功能，支持分页，高亮
    public List<Map<String, Object>> searchGoods(@RequestParam(name = "keyword") String keyword,@RequestParam(defaultValue = "0",name = "pageNo") int pageNo,@RequestParam(defaultValue = "20",name = "pageSize") int pageSize)
            throws Exception {

        SearchRequest request = new SearchRequest("jd_goods");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);
        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        highlightBuilder.requireFieldMatch(false);
        highlightBuilder.field("name");
        searchSourceBuilder.highlighter(highlightBuilder);

        //精确查询
        MatchQueryBuilder name1 = QueryBuilders.matchQuery("name", keyword);

        searchSourceBuilder.query(name1);
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        // 解析结果
        ArrayList<Map<String, Object>> list = new ArrayList<>();

        for (SearchHit hit : response.getHits().getHits()) {
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlightField = highlightFields.get("name");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //原来的结果
            // 解析高亮字段
            if (highlightField!=null){
                Text[] fragments = highlightField.getFragments();
                StringBuilder name= new StringBuilder();
                for (Text fragment : fragments) {
                    name.append(fragment);
                }
                sourceAsMap.put("name", name.toString()); //高亮字段替换原来的
            }
            list.add(sourceAsMap);
        }
        return list;
    }

}
